// pages/person/person.js


Page({

  data: {
    //登录信息获取
    userInfo: {}, // 存储用户信息列表
    hasUserInfo: false,
    logged: false,
    takeSession: false,
    requestResult: '', // 请求结果
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') ,// 如需尝试获取用户信息可改为false
    ava:'',
    nickName:'',
    //登录授权切换
    hidden1:false,//person
    hidden2:true,//person01
    hidden3:true,//all_volunteer
    volhidden:true,//volunteer vol
    oldhidden:true,//volunteer old

    //person01
    //volunteer
    hour:'',
    rank:'',
    star:[{url:'/static/image/星星无.png'},
          {url:'/static/image/星星无.png'},
          {url:'/static/image/星星无.png'},
          {url:'/static/image/星星无.png'},
          {url:'/static/image/星星无.png'}]
  },

  onShow:function(){
    var userInfo = wx.getStorageSync('userInfo')
    this.setData({
      ava:userInfo.avatarUrl,
      nickName:userInfo.nickName
    })
    if(wx.getStorageSync('identity') === 'old'){
      this.setData({
        hidden1:true,
        hidden2:true,
        hidden3:false,
        volhidden:true,
        oldhidden:false
      })
    }
    if(wx.getStorageSync('identity') === 'vol'){
      this.setData({
        hidden1:true,
        hidden2:true,
        hidden3:false,
        volhidden:false,
        oldhidden:true
      })
    }
  },

  onLoad:function(){
    wx.request({
      url: 'http://127.0.0.1:8082/demo1/superadmin/listvolunteerbyhour',
      data:{},
      method:'GET',
      success:(res)=>{
        console.log(res.data.volunteerListByHour)
        var volunteer = res.data.volunteerListByHour
        var len = volunteer.length
        for(var i=0;i<len;i++){
          if(volunteer[i].volid == wx.getStorageSync('openid')){
            wx.setStorageSync('volunteer', volunteer[i])
            // console.log(wx.getStorageSync('volunteer'))
            this.setData({
              hour:volunteer[i].volhour,
              rank:i+1
            })
            var hour = this.data.hour
            var len = 0
          if(hour>=100 && hour<350){
            len = 1
          }else if(hour>=350 && hour < 600){
            len = 2
          }else if(hour>=600 && hour < 850){
            len = 3
          }else if(hour>=850 && hour < 1100){
            len = 4
          }else if(hour>1100){
            len = 5
          }
           for(var i = 0;i<len;i++){
             this.data.star[i].url = '/static/image/星星.png'
           }
           this.setData({
             star:this.data.star
           })
          }
          
        }
      }
    })

  },

  //登陆跳转
  login: function (e) { 
    var that = this
    //获取用户信息
    wx.getUserProfile({
      lang: 'zh_CN',
      desc: '用于在后台更好的识别您的身份', 
      success(res) {
        //用户信息 
        console.log('获取', res)
        let userInfo = res.userInfo
        wx.setStorageSync('userInfo', userInfo); 
        that.setData({
          ava:userInfo.avatarUrl,
          nickName:userInfo.nickName
        })

          //login获取code 然后request获取openid
          wx.login({
            success:(res) =>{
              wx.setStorageSync('identity', '')
              //code信息
              console.log(res)
              let code = res.code;
              wx.request({
                url: `https://api.weixin.qq.com/sns/jscode2session?appid=wx6364fd65abca6cb1&secret=a7ca79e76d2af4a2c9b78a35ff707e2b&js_code=${code}&grant_type=authorization_code`,
                success:(res)=>{
                  //openid sessionkey
                  console.log(res);
                  wx.setStorageSync('openid', res.data.openid)
        
                  var tempid = res.data.openid
                  //openid
                  console.log(tempid)
                  wx.request({
                    url: "http://127.0.0.1:8082/demo1/superadmin/getvolunteerbyid",
                    data:{"volunteerId":tempid},
                    method:'GET',
                    success:(res)=>{
                        console.log(res)

                        if(res.data.volunteer != null){
                          wx.setStorageSync('identity', 'vol')
                          that.setData({
                            hidden1:true,
                            hidden2:true,
                            hidden3:false,
                            volhidden:false,
                            oldhidden:true
                          })
                        }
                        else{
                          wx.request({
                            url: "http://127.0.0.1:8082/demo1/superadmin/getolderbyid",
                            data:{"olderId":tempid},
                            method:'GET',
                            success:(res)=>{
                              console.log(res)
                              if(res.data.older != null){
                                wx.setStorageSync('identity', 'old')
                                that.setData({
                                  hidden1:true,
                                  hidden2:true,
                                  hidden3:false,
                                  volhidden:true,
                                  oldhidden:false
                                })
                              }else{
                                that.setData({
                                  hidden1:true,
                                  hidden2:false
                                })
                              }
                            },
                            fail:(err)=>{
                                console.log(err)
                            }
                          })
                      }
                    },
                    fail:(err)=>{
                      console.log(err)
                    }
                  })
                }
              })
            }
          })
          // wx.switchTab({ 
          //   url: "/pages/person01/person01.wxml",
          // }); 
        // 进入到首页后，出现消息提示窗，提示用户:'欢迎使用本小程序'的提示语
        wx.showToast({
          image: "/static/image/success.png", 
          icon: "success", 
          title: '欢迎使用',
          duration: 1000, 
        });
      },
      fail(err) { //接口调用失败的回调函数 用户拒绝授权登录后，出现的提示窗
        console.error(err) //打印输出错误数据
      // 拒绝登录 显示消息提示框 
        wx.showToast({ 
          image: "/static/image/error.png",
          title: '用户拒绝授权', 
          icon: "error",
          duration: 1000
        });
      }
    })


    
 },

//oYIiY4lNt_M0EJB2mrQPHenGsTBg
loginout:function(){
  
  wx.clearStorage({
    success: (res) => {
      this.setData({
        hidden1:false,
        hidden2:true,
        hidden3:true,
        volhidden:true,
        oldhidden:true,
      })
      wx.showToast({
    icon:'success',
    title: '退出成功',
  })
    },
  })
  
},

 
 changeImage: function () {
  var _this = this;
  wx.chooseImage({
    count: 1,   // 可选相片数量，最多9张
    sizeType: ['original', 'compressed'], // 可选“原图”、“压缩图”
    sourceType: ['album', 'camera'],  // 可选“相册”、“相机”
    success(res) {
      console.log(res.tempFilePaths);
      // tempFilePath可以作为img标签的src属性显示图片
      const tempFilePaths = res.tempFilePaths;
      if(tempFilePaths.length > 0){
        _this.setData({
          avatarUrl: tempFilePaths[0]
        })
      }
    }
  })
},

enroll: function(){
  wx.navigateTo({
    url: '/pages/enroll/enroll',
  })
},

enroll_old: function(){
  wx.navigateTo({
    url: '/pages/enroll_old/enroll_old',
  })
},
//volunteer
exchange:function(){
  wx.navigateTo({
    url: '/pages/exchange/exchange',
  })
},


/*older */
calladmin:function(){
  wx.showModal({
    title:'确定拨通客服?',
    success:(res)=>{
      if(res.confirm){
        wx.makePhoneCall({
    phoneNumber: '10124560210',
  })
      }
    }
  })
},
//detial
detial:function(){
  wx.navigateTo({
    url: '/pages/detial/detial',
  })
},
//certificate
certificate:function(){
  wx.navigateTo({
    url:'/pages/certificate/certificate',
  })
},


})